<?php

namespace common\rewrites;

use common\Tools;
use yii\web\IdentityInterface;

/**
 * @property string $loginIp
 */
class User extends \yii\web\User
{
    public $loginIp = '0.0.0.0';

    /**
     * @param IdentityInterface $identity
     * @param int $duration
     * @return bool
     */
    public function login(IdentityInterface $identity, $duration = 0)
    {
        if ($this->beforeLogin($identity, false, $duration)) {
            $this->switchIdentity($identity, $duration);
            if (!Tools::isCli()) {
                $this->loginIp = \Yii::$app->request->getUserIP();
            }
            $this->afterLogin($identity, false, $duration);
        }
        return !$this->getIsGuest();
    }
}